// src/database/mysql/mysql.module.ts
import { Module, Global } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MysqlService } from './mysql.service';
import { Cat } from './entity/cat.entity';
import { Dog } from './entity/dog.entity';

@Global()
@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: () => ({
        type: 'mysql',
        host: process.env.MYSQL_HOST,
        port: parseInt(process.env.MYSQL_PORT, 10),
        username: process.env.MYSQL_USER,
        password: process.env.MYSQL_PASSWORD,
        database: process.env.MYSQL_DATABASE,
        entities: [__dirname + './../../**/*.entity{.ts,.js}'],
        synchronize: true, // 开发环境可开启，生产建议关闭
      }),
    }),
    TypeOrmModule.forFeature([Cat, Dog]),
  ],
  providers: [MysqlService],
  exports: [MysqlService],
})
export class MysqlModule {}
